<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button v-if="isAuth('kernel:menu:form')" type="success" icon="Plus" @click="router.push({ name: 'kernel_menu_form' })">
              {{ i18n('common.create') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_top">
        <div class="admin_main_block_select">
          <div class="input">
            <el-select v-model="dataForm.category_id" :placeholder="i18n('common.please_select') + i18n('menu.category_info')" clearable>
              <el-option :label="i18n('common.all')" value=""></el-option>
              <el-option v-for="(v, k) in categoryList" :label="v.title" :key="k" :value="v.id"></el-option>
            </el-select>
          </div>
          <div class="input">
            <el-button icon="Search" @click="getDataList(true)">
              {{ i18n('common.search') }}
            </el-button>
          </div>
        </div>
      </div>

      <el-table :data="dataList" row-key="id" border style="width: 100%">
        <el-table-column prop="remark" header-align="center" min-width="150" :label="i18n('menu.title')">
        </el-table-column>

        <el-table-column header-align="center" align="center" :label="i18n('menu.icon')">
          <template v-slot="scope">
            <i v-if="scope.row.icon" :class="'el-custom-icon-' + scope.row.icon || ''" width="15"></i>
          </template>
        </el-table-column>

        <el-table-column prop="category" header-align="center" align="center" :label="i18n('menu.category_info')">
          <template v-slot="scope">
            <span v-if="scope.row.category">{{ scope.row.category.title }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="type" header-align="center" align="center" :label="i18n('menu.type')">
          <template v-slot="scope">
            {{ scope.row.type.text }}
          </template>
        </el-table-column>

        <el-table-column prop="url" header-align="center" align="center" :show-overflow-tooltip="true" :label="i18n('menu.url')">
          <template v-slot="scope">
            {{ scope.row.url || i18n('common.omit') }}
          </template>
        </el-table-column>

        <el-table-column fixed="right" header-align="center" align="center" width="200" :label="i18n('common.handle')">
          <template v-slot="scope">
            <el-button v-if="isAuth('kernel:menu:form')" type="primary" icon="Edit" @click="router.push({ name: 'kernel_menu_form', query: { id: scope.row.id } })">
              {{ i18n('common.update') }}
            </el-button>

            <el-button v-if="isAuth('kernel:menu:delete')" type="danger" icon="Delete" @click="deleteHandle(scope.row.id)">
              <span class="delete">{{ i18n('common.delete') }}</span>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script setup name="menu-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"
  import { treeDataTranslate } from '@/utils/library'

  const { router, i18n, isAuth } = useBase()

  const model = ref('menu')
  const categoryList = ref([])
  const dataForm = reactive([
    'category_id',
  ])

  const {
    dataList,
    dataListLoading,
    deleteHandle,
  } = useTable(model.value, dataForm)


  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-10
   *
   * 获取菜单数据列表
   *
   * @return {[type]}
   */
  const getDataList = () => {
    dataListLoading.value = true
    $http({
      url: $http.adornUrl('/menu/list'),
      method: 'get',
      params: $http.adornParams({
        category_id: dataForm.category_id,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        dataList.value = treeDataTranslate(data.data, 'id')
      }
      dataListLoading.value = false
    })
  }


  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-10
   *
   * 获取菜单分类数据列表
   *
   * @return {[type]}
   */
  const getCategoryList = () => {
    $http({
      url: $http.adornUrl('/menu/category/select'),
      method: 'get',
      params: $http.adornParams(),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        categoryList.value = data.data
      }
    })
  }

  onMounted(() => {
    getDataList(model)
    getCategoryList()
  })
</script>
